﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Dict.DataModel;
using ProtoBuf.Message;

namespace UI
{
    public class UIActivityTreasureCast : UIActivityTemplate
    {
        public UILabel surplusNumber;
        public GameObject time;
        public UILabel totlaCoin;
        public UILabel coinCount;
        public GameObject btn;
        public GameObject normalPic;
        public GameObject finishedPic;
        public GameObject jinbi;
        public GameObject timesRunOut;
        public GameObject solgon;
        public GameObject solgon2;
        public const int TODAYCOUNT = 3;
        private ActivityFortuneCatData data;

        private long targetTime;

        public override void Show()
        {
            //string acid = this.activityId;
            CSFortuneCatInfo msg = new CSFortuneCatInfo();
            NetAPI.SendHttp(OpDefine.CSFortuneCatInfo, msg, FortuneCatInfoCallBack);
            base.Show();
        }

        protected override void PrepareView()
        {
            base.PrepareView();
        }
        void FortuneCatInfoCallBack(NetWork.Packets.Packet packet, bool isSuccess)
        {
            if (isSuccess)
            {
                SCFortuneCatInfo msg = packet.kBody as SCFortuneCatInfo;
                data = msg.info;
                UpdateState(data);
            }
        }

        public void RewardButtonClick()
        {
            if (data.todayLeft != 0 || data.totalStatus == 1)
            {
                CSFortuneCatReward msg = new CSFortuneCatReward();
                msg.type = data.totalStatus;
                NetAPI.SendHttp(OpDefine.CSFortuneCatReward, msg, ButtonClickCallBack);
            }
            else
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("acRestZero"));
            }
        }

        void ButtonClickCallBack(NetWork.Packets.Packet packt, bool isSuccess)
        {
            StaticData.redTips.SetPointState(RedPointTip.Red.fortuneCat, 0);
            StaticData.redTips.ReduceActivitys();
            SCFortuneCatReward msg = packt.kBody as SCFortuneCatReward;
            data = msg.info;

            ResultObject result = msg.get;
            StartCoroutine(PlayAnimationAndShowGet(result));
            StaticData.playerData.AddSimpleItem(msg.get.simps);
            UpdateState(data);
        }
        IEnumerator PlayAnimationAndShowGet(ResultObject obj)
        {
            jinbi.SetActive(true);
            yield return new WaitForSeconds(3.0f);
            CommonMethod.ShowGetObjs(obj);
            jinbi.SetActive(false);
        }
        public void UpdateState(ActivityFortuneCatData activityData)
        {
            data = activityData;
            if (data.totalStatus == 1)
            {
                normalPic.SetActive(false);
                solgon2.SetActive(false);
                finishedPic.SetActive(true);
                solgon.transform.localPosition = new Vector3(-197, -57, 0);
            }
            else if (data.totalStatus == 0)
            {
                solgon.SetActive(true);
                solgon2.SetActive(true);
                solgon.transform.localPosition = new Vector3(-208, -5, 0);
            }
            else if (data.totalStatus == 2)
            {
                solgon.SetActive(false);
                btn.SetActive(false);
                timesRunOut.SetActive(true);
            }
            if (data.thisTimeStatus == 2)
            {
                btn.GetComponent<UIImageButton>().isEnabled = false;
            }

            surplusNumber.text = activityData.todayLeft + "/" + TODAYCOUNT;
            targetTime = activityData.nextGetTime;
            totlaCoin.text = activityData.coinPool.ToString();
            coinCount.text = (activityData.totalLeft).ToString();
            if (activityData.thisTimeStatus == 0)
            {
                time.SetActive(true);
            }
            StopCoroutine("UpdateUI");
            StartCoroutine("UpdateUI");
        }
        void ComputeRewardTime()
        {
            if (data == null)
                return;
            long curtime = ServerTimerTool.CurrentUTCJavaTime();
            if (curtime < targetTime && data.todayLeft != 0)
            {
                time.SetActive(true);
                btn.GetComponent<UIImageButton>().isEnabled = false;
                int interval = (int)(targetTime - curtime);
                int s = (interval / 1000) % 60;
                int m = (interval / 60000) % 60;
                string str = m.ToString("d2") + ":" + s.ToString("d2");
                time.GetComponent<UILabel>().text = string.Format(TableTextBlo.GetText("activity_treasure_time"), str);

            }
            else
            {
                time.SetActive(false);
                btn.GetComponent<UIImageButton>().isEnabled = true;
            }

            if (data.todayLeft != 0)
            {
                if (curtime > targetTime)
                {
                    StaticData.redTips.SetPointState(RedPointTip.Red.fortuneCat, 1);
                }
                else
                {
                    StaticData.redTips.SetPointState(RedPointTip.Red.fortuneCat, 0);
                    //StaticData.redTips.ReduceActivitys();
                }
            }
        }

        IEnumerator UpdateUI()
        {
            while(true)
            {
                ComputeRewardTime();
                yield return new WaitForSeconds(1);
            }
        }
    }
}